﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMP_QuanLySV.DAL;

namespace SMP_QuanLySV.BLL
{
    class BLLKhoa
    {

        List<Khoa> list;
        QuanLySVDataContext db;


        // constructor
        public BLLKhoa()
        {
            this.list = new QuanLySVDataContext().Khoas.ToList();
            this.db = new QuanLySVDataContext();
        }

        // kiem tra 1 Khoa co ton tai hay khong
        public bool checkExist(Khoa temp)
        {
            foreach (Khoa dt in list)
            {
                if (dt.KhoaID.Equals(temp.KhoaID))
                    return true;
            }
            return false;
        }

        // update thong tin Khoa
        public bool updateKhoa(Khoa update)
        {
            try
            {

                var temp = (from c in db.Khoas
                            where c.KhoaID == update.KhoaID
                            select c).First();
                Khoa found = (Khoa)temp;
                found.KhoaID = update.KhoaID;
                found.TenKhoa = update.TenKhoa;
               
                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // them Khoa moi vao database
        public bool insertKhoa(Khoa temp)
        {
            try
            {
                db.Khoas.InsertOnSubmit(temp);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // xoa Khoa
        public bool deleteKhoa(Khoa temp)
        {
            try
            {
                var dt = (from c in db.Khoas
                          where c.KhoaID == temp.KhoaID
                          select c).First();
                Khoa found = (Khoa)dt;

                db.Khoas.DeleteOnSubmit(found);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // tra ve danh sach cac khoa
        public List<Khoa> returnListKhoa()
        {
            this.list = new QuanLySVDataContext().Khoas.ToList();
            return list;
        }

        // tra ve 1 khoa theo so thu tu trong list
        public Khoa retrieveByIndex(int i)
        {
            this.list = new QuanLySVDataContext().Khoas.ToList();
            if (i >= 0 && i < list.Count)
                return list[i];
            else return null;
        }

        // tra ve 1 khoa theo ma khoa
        public Khoa retrieveByID(string ID)
        {
            this.list = new QuanLySVDataContext().Khoas.ToList();
            foreach (Khoa temp in list)
                if (temp.KhoaID == ID)
                {
                    return temp;
                }
            return null;
        }

        // kiem tra thong tin 1 khoa co hop le hay khong
        public bool kiemTra(Khoa temp)
        {
            if (temp.KhoaID == "") return false;
            return true;
        }
    }
}
